<?php
class EditBadWordForm extends Form
{	
	function EditBadWordForm(){	
		Form::Form('EditBadWordForm');
		CGlobal::$website_title= (AZLib::getParam('cmd')=='edit')?"Sửa Từ Xấu":"Thêm Từ Xấu";
	}
	function on_submit(){				
		$id = intval(AZLib::getParam('id'));
		$cmd= AZLib::getParam('cmd');		
		$exact = AZLib::getParam('exact');
		$is_phone = AZLib::getParam('is_phone');
				
//		$contents = trim(AZLib::getParam('contents'));
//		$contents = AZLib::delDoubleSpace(AZLib::trimSpace($contents));	
//		$contents = trim($contents,",");

		$contents = Url::get("contents");
		$reason = Url::get("reason");				
		
		if($is_phone==1){			
			$contents = BadWord::badword_phone_type($contents);
		}
				
		$where = "";		
		if($cmd=='edit' && $id && $contents){
			$where = " AND id <> $id";
		}	
					
		$re = DB::query("SELECT id FROM bad_words WHERE checksum = '".md5($contents)."' ".$where);
		$item = mysql_fetch_assoc($re);				
		
		if($item["id"]){
			$this->setFormError('adv_banner',"<b>Từ khóa '$contents' đã tồn tại.</b>");
			return false;
		}				
			
		if($cmd=='edit' && $id && $contents){		
			if(DB::query('UPDATE bad_words SET contents="'.$contents.'", exact="'.$exact.'", reason="'.$reason.'", is_phone="'.$is_phone.'", checksum="'.md5($contents).'" WHERE id="'.$id.'"')){			
				AZLib::get_badword(1,0);
			}
		}
		elseif($cmd=='add' && $contents){
					
			if(DB::query('INSERT INTO bad_words(contents,exact,is_phone,checksum,reason) VALUES ("'.$contents.'","'.$exact.'","'.$is_phone.'","'.md5($contents).'","'.$reason.'")')){
				AZLib::get_badword(1,0);
			}			
		}				

		Url::redirect_current();	
	}	
	function draw(){
		
		$this->beginForm();
		if(Url::get('cmd')=='edit' && $row = DB::select('bad_words','id='.intval(AZLib::getParam('id')))){						
			$contents = $row['contents'];
			$reason = $row['reason'];
			$exact = $row['exact'];
			if($row['is_phone']==1){						
				$contents = BadWord::filter_badword_show($contents);
			}
			$is_phone = $row['is_phone'];
		}else{
			$contents = '';
			$exact = '';
			$is_phone = '';	
			$reason = '';	
		}
		
		global $display;
		
		$display->add('msg',$this->showFormErrorMessages());			
		$display->add('contents',$contents);
		$display->add('reason',$reason);
		$display->add('exact',$exact);
		$display->add('is_phone',$is_phone);
		$display->add('cmd',Url::get('cmd'));		
		$display->output('edit');
		$this->endForm();
	}
		
}
?>